package com.basedefence;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.microedition.khronos.opengles.GL10;

import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.util.adt.io.in.IInputStreamOpener;


public class TextureManager
{
	private static HashMap<String, ITexture > texturesMap = new HashMap<String, ITexture >();
	
	/*
	 * Return texture. It must be loaded first.
	 */
	public static ITexture  getTexture (String name)
	{
		return texturesMap.get(name);
	}
	
	/*
	 * Return texture. Load it, if needed.
	 */
	public static ITexture getTexture (String name, int width, int height, int xindex, int yindex, int border, int margin)
	{
		ITexture texture = getTexture(name);

		try {
			if (texture == null)
				texture = loadTexture(name, width, height, xindex, yindex,
						border, margin);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return texture;
	}
	
	public static ITexture loadTexture (String name, int width, int height) throws IOException {
		return loadTexture(name, width, height, 0, 0, 0, 0);
	}
	
	public static ITexture loadTexture (String name, int width, int height, int xindex, int yindex, int border) throws IOException {
		return loadTexture(name, width, height, xindex, yindex, border, border);
	}
	
	public static ITexture loadTexture (final String name,final int width,final int height,final int xindex,final int yindex,final int border,final int margin) throws IOException
	{
//		TiledTexture texture = new TiledTexture(name, width, height, xindex, yindex, border, margin, null, Option.DEFAULT);
		ITexture texture = new BitmapTexture(BaseDefenceActivity.getInstance().getTextureManager(), new IInputStreamOpener() {
	        @Override
	        public InputStream open() throws IOException {
	            return BaseDefenceActivity.getInstance().getAssets().open(name);
	        }
	    });
		texturesMap.put(name, texture);
		return texture;
	}
	
	public static void unloadTexture (String name)
	{
		texturesMap.remove(name);
	}
}
